fix: reduce session.messages() calls with event-based caching to prevent memory leaks#1787
Conversation
…ent memory leaks - Replace session.messages() fetch in context-window-monitor with message.updated event cache - Replace session.messages() fetch in preemptive-compaction with message.updated event cache - Add per-session transcript cache (5min TTL) to avoid full rebuild per tool call - Remove session.messages() from background-agent polling (use event-based progress) - Add TTL pruning to todo-continuation-enforcer session state Map - Add setInterval.unref() to tool-input-cache cleanup timer Fixes code-yeongyu#1222
|
I have read the CLA Document and I hereby sign the CLA |
🔍 Detailed Analysis: Why This Fix MattersThe ProblemI noticed memory usage growing continuously during extended sessions with oh-my-opencode. On a 62GB RAM server, the opencode process reached ~5GB RSS in just 15 minutes, increasing at ~30MB/s. Investigation revealed the root cause: What happens on every single tool call (before this fix):In a session with 200+ messages, each fetch returns several MB of JSON. Since this happens on every tool call, memory balloons rapidly. What this PR does:The key insight is that
The approach is minimal and low-risk — no architectural changes, just removing unnecessary API calls and using data that is already available via events. Before → After
This should address the core memory issue reported in #1222, #1486, and #361. |
There was a problem hiding this comment.
No issues found across 9 files
Confidence score: 5/5
- Automated review surfaced no issues in the provided summaries.
- No files require special attention.
Requires human review: Significant logic changes including removal of stability detection in background-agent and new caching layers. Cannot guarantee 100% no regressions without validation of event-based progress tracking.
|
Excellent work on this! The memory leak analysis was thorough and the fix is surgical — replacing redundant session.messages() calls with event-based caching is exactly the right approach. The detailed before/after table in your description made reviewing a breeze. Thank you! 🙏 |
Summary
Eliminate redundant
session.messages()API calls in hot paths to fix memory leaks during extended sessions.Root Cause
Multiple hooks call
session.messages()(full session history fetch) on every tool execution, causing memory to grow proportionally with session length. In a 200+ message session, each tool call triggered 3-4 full fetches of the entire message history.Changes
context-window-monitor.tssession.messages()withmessage.updatedevent cachepreemptive-compaction.tstranscript.tsbackground-agent/manager.tssession.messages()from polling looptodo-continuation-enforcer/session-state.tstool-input-cache.tssetInterval.unref()Impact
session.messages()calls per tool execution + continuous polling fetchesTesting
bun run typecheck✅bun run build✅Verification
Related Issues
Fixes #1222
Related: #1769, #1752, #1486, #361
Summary by cubic
Replaced repeated session.messages() fetches with event-based caching to stop memory leaks in long sessions and cut API calls to near-zero during tool runs. Addresses #1222 by moving hot paths to use message.updated data and adding TTL-based cleanup.
Written for commit eb56701. Summary will update on new commits.